package com.zzb.framework.query.sqlbuilder;

import com.baomidou.mybatisplus.annotation.TableField;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 通过Field分析获取SQL
 * Created by zhangzhenbin on 17-8-9.
 */
public class OrderByBuilder {
    /**
     * 通过Fields获取Order by字段
     *
     * @param allDeclaredFields
     * @return
     */
    public static String getOrderByFields(Field[] allDeclaredFields) {
        String orderBy = "";
        for (Field field : allDeclaredFields) {
            TableField columnAnnotation = field.getAnnotation(TableField.class);
            if (columnAnnotation == null) continue;
            String columnName = columnAnnotation.value();
            if (field.getName().equals("id")) {
                orderBy = "id";
            }
            if (field.getType() == Date.class) {
                if (orderBy == null || orderBy.equals("id") || field.getName().contains("create")) {
                    orderBy = columnName;
                }
            }
        }
        Map<String, Field> fieldMap = Arrays.stream(allDeclaredFields).collect(Collectors.toMap(Field::getName, o -> o));
        if (StringUtils.isNotBlank(orderBy) ) {
            orderBy = orderBy + " desc";
        }

        if (StringUtils.isNotBlank(orderBy)) {
            String columnName;
            int descIndex;
            if ((descIndex = orderBy.lastIndexOf("desc")) == -1) {
                descIndex = orderBy.lastIndexOf("asc");
            }
            String[] split = orderBy.substring(0, descIndex).split(" ");
            if (ArrayUtils.isNotEmpty(split)) {
                for (String name : split) {
                    if (name.equals(",") || name.equals("desc") || name.equals("asc")) continue;
                    if (name.contains(",")) {
                        for (String s : name.split(",")) {
                            try {
                                Field declaredField = fieldMap.get(s);
                                TableField columnAnnotation = declaredField.getAnnotation(TableField.class);
                                columnName = columnAnnotation.value();

                            } catch (Exception e) {
                                columnName = s;
                            }
                            orderBy = orderBy.replace(s, columnName);

                        }
                    } else {
                        try {
                            Field declaredField = fieldMap.get(name);
                            TableField columnAnnotation = declaredField.getAnnotation(TableField.class);
                            columnName = columnAnnotation.value();

                        } catch (Exception e) {
                            columnName = name;
                        }
                        orderBy = orderBy.replace(name, columnName);

                    }

                }
            }

        }
        String orderString;
        if (!orderBy.contains("order by") && StringUtils.isNotEmpty(orderBy))
            orderString = " order by " + orderBy;
        else
            orderString = orderBy;

        return orderString;
    }

}
